﻿using System;
#if !PHONE
using System.Windows.Browser;
#endif
using System.Diagnostics;
using System.Collections.Generic;
using System.Windows.Threading;

namespace Microsoft.Popfly.GameCreator.GameEngine
{
    public enum ConsoleFormat
    {
        None,
        Heading,
        Warning,
        Error,
    }

    public static class Console
    {
        private static readonly TimeSpan ConsolePublishRate = TimeSpan.FromMilliseconds(250);
        private static readonly Queue<LogStatement> _messageQueue = new Queue<LogStatement>();
        private static readonly DispatcherTimer _publishTimer;
        private static readonly object _syncObject = new object();

        static Console()
        {
            _publishTimer = new DispatcherTimer { Interval = ConsolePublishRate };
            _publishTimer.Tick += (sender, e) => PublishMessages();
            _publishTimer.Start();
        }
        public static SilverlightConsole SilverlightConsole;

        /// <summary>
        /// JavaScript signature
        /// </summary>
        public static void write(string message)
        {
            Write(message);
        }

        public static void Write(string message)
        {
            if (message == null)
                throw new ArgumentNullException("message");

            Write(message, ConsoleFormat.None);
        }

        /// <summary>
        /// JavaScript signature
        /// </summary>
        public static void write(string message, string format)
        {
            if (format == null)
                throw new ArgumentNullException("format");

            ConsoleFormat consoleFormat = ConsoleFormat.None;
            try
            {
                consoleFormat = (ConsoleFormat)Enum.Parse(typeof(ConsoleFormat), format, true);
            }
            catch (ArgumentException) { }

            Write(message, consoleFormat);
        }

        public static void Write(string message, ConsoleFormat format)
        {
            if (message == null)
                throw new ArgumentNullException("message");

            Debug.WriteLine(message);

            lock (_syncObject)
                _messageQueue.Enqueue(new LogStatement { Message = message, Format = format });
        }

        public static void ReportError(string message)
        {
            SilverlightConsole.ReportError(message);
        }

        public static void IssueWarning(string message)
        {
            SilverlightConsole.IssueWarning(message);
        }

        public static void Show()
        {
            SilverlightConsole.Show();
        }

        public static void Hide()
        {
            SilverlightConsole.Hide();
        }
        public static void PublishMessages()
        {
            lock (_syncObject)
            {
                foreach (LogStatement statement in _messageQueue)
                {
                    SilverlightConsole.Write(statement.Message, statement.Format);
                }
                _messageQueue.Clear();
            }

        }

        private struct LogStatement
        {
            public ConsoleFormat Format { get; set; }
            public string Message { get; set; }
        }
    }
}
